package com.weblearning.tliasweb.controller;

import com.weblearning.tliasweb.pojo.Dept;
import com.weblearning.tliasweb.pojo.Result;
import com.weblearning.tliasweb.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    //@RequestMapping(value = "/depts",method = RequestMethod.GET)  //请求方式：为get
    @GetMapping
    public Result list(){
        log.info("查询所有部门");
        List<Dept> deptlist = deptService.list();
        return  Result.success(deptlist);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除"+id+"部门");
        deptService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("添加新部门"+dept);
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result select(@PathVariable Integer id){
        log.info("查询id="+id+"部门");
        Dept dept = deptService.select(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("更新部门"+dept);
        deptService.update(dept);
        return Result.success();
    }

}
